package com.sgb.kjwl.view.ui.adapter.home;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemCarHallSubVideoBinding;
import com.sgb.kjwl.databinding.ItemLeaseHallVideoBinding;
import com.sgb.kjwl.databinding.ItemMachineListBinding;
import com.sgb.kjwl.model.entity.lease.LeaseListEntity;
import com.sgb.knowledgeshare.utils.SharePermissionUtils;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.knowledgeshare.view.activity.HomeShareActivity;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class LeaseListAndVideoAdapter extends BaseMultiItemQuickAdapter<LeaseListEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public LeaseListAndVideoAdapter(List<LeaseListEntity> data) {
        super(data);
        // 租赁类别
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.item_machine_list);
        //视频类别
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.item_lease_hall_videos);
        //单个视频类别
        addItemType(SingleItemConstant.TYPE_THREE, R.layout.item_lease_hall_video);
    }

    @Override
    protected void convert(BaseViewHolder helper, LeaseListEntity item) {
        switch (item.getItemType()) {
            case SingleItemConstant.TYPE_ONE:
                initCarInfo(helper, item);
                break;
            case SingleItemConstant.TYPE_TWO:
                initVideoList(helper, item);
                break;
            case SingleItemConstant.TYPE_THREE:
                initSingleVideo(helper, item);
                break;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

                @Override
                public int getSpanSize(int position) {
                    if(getItemViewType(position) == SingleItemConstant.TYPE_ONE){
                        return 1;
                    }else{
                        return 2;
                    }

                }
            });
        }
    }

    /**
     * 租赁信息
     */
    private void initCarInfo(BaseViewHolder helper, LeaseListEntity entity) {
        //设置dataBinding数据
        ItemMachineListBinding binding = DataBindingUtil.bind(helper.itemView);
        if (binding == null) return;
        binding.setVariable(BR.viewModel, entity);
        binding.executePendingBindings();
        binding.tvArea.setText(entity.getCity() + entity.getArea());

        if ("1".equals(entity.getNegotiable())) {
            binding.priceView.setText("面议");
        } else {
            binding.priceView.setText(entity.getPrice() + " " + entity.getPriceUnit());
        }
        if (!TextUtils.isEmpty(entity.getDistance())) {
            binding.tvDistance.setVisibility(View.VISIBLE);
            binding.tvDistance.setText(entity.getDistance());
        } else {
            binding.tvDistance.setVisibility(View.GONE);
        }

        if ("1".equals(entity.getIsSelf())) {//是否自营，1为自营
            SpannableString spannableString = new SpannableString("  " + entity.getTitle());
            Drawable d = helper.itemView.getContext().getResources().getDrawable(R.mipmap.ic_lease_isself);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            spannableString.setSpan(new ImageSpan(d), 0, 1, ImageSpan.ALIGN_BASELINE);
            binding.titleView.setText(spannableString);
        } else {
            binding.titleView.setText(entity.getTitle());
        }

//        try {
//            binding.tvTime.setText(DateUtils.stampToDay(entity.getUpdatetime()));
//        } catch (Exception e) {
//
//        }

    }

    /**
     * 设置视频信息
     */
    private void initVideoList(BaseViewHolder helper, LeaseListEntity entity) {
        RecyclerView recyclerView = helper.getView(R.id.rv_item_vehicle_hall_videos);
        List<VideoBean> videoBeanList = new ArrayList<>();
        videoBeanList.addAll(entity.getVideoProDict());
        if (videoBeanList.size() >= 5) {
            VideoBean v = new VideoBean();
            v.setType("查看更多视频");
            videoBeanList.add(v);
        }
        CarsHallVideoAdapter adapter = new CarsHallVideoAdapter(videoBeanList);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        if (recyclerView.getItemDecorationCount() == 0) {
            DividerDecoration.builder(mContext)
                    .color(Color.TRANSPARENT)
                    .size(10, TypedValue.COMPLEX_UNIT_DIP)
                    .build()
                    .addTo(recyclerView);
        }
        adapter.bindToRecyclerView(recyclerView);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            if (adapter.getData().get(position).isCheckMoreVideos()) {
                SharePermissionUtils.getPermissionTwo((Activity) mContext,1);
            } else {
                //视频播放
                Intent intent = new Intent(mContext, PlayListActivity.class);
                intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) entity.getVideoProDict());
                intent.putExtra(PlayListActivity.PSO, position);
                intent.putExtra(PlayListActivity.VIDEOTYPE, "home_lease");
                mContext.startActivity(intent);
            }
        });
    }

    /**
     * 设置单个视频信息
     */
    private void initSingleVideo(BaseViewHolder helper, LeaseListEntity entity) {
        //设置dataBinding数据
        ItemLeaseHallVideoBinding binding = DataBindingUtil.bind(helper.itemView);
        if (binding == null) return;
        if (entity.getVideoProDict() != null && !entity.getVideoProDict().isEmpty()) {
            binding.setVariable(BR.viewModel, entity.getVideoProDict().get(0));
            binding.executePendingBindings();
        }
    }


    /**
     * 视频适配器
     */
    private static class CarsHallVideoAdapter extends BaseQuickAdapter<VideoBean, BaseViewHolder> {

        public CarsHallVideoAdapter(@Nullable List<VideoBean> data) {
            super(R.layout.item_car_hall_sub_video, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, VideoBean entity) {

            if (entity.isCheckMoreVideos()) {
                ((ImageView) helper.getView(R.id.tag_cuxiao)).setImageDrawable(mContext.getResources().getDrawable(R.drawable.check_more_video));
                ((ImageView) helper.getView(R.id.iv_item_car_hall_video)).setImageDrawable(mContext.getResources().getDrawable(R.drawable.gray_radius_bkg));
                (helper.getView(R.id.more_video_text)).setVisibility(View.VISIBLE);
                (helper.getView(R.id.title_view)).setVisibility(View.GONE);
            } else{
                //设置dataBinding数据
                (helper.getView(R.id.title_view)).setVisibility(View.VISIBLE);
                (helper.getView(R.id.more_video_text)).setVisibility(View.GONE);
                ((ImageView) helper.getView(R.id.tag_cuxiao)).setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icn_video_play));
                ItemCarHallSubVideoBinding binding = DataBindingUtil.bind(helper.itemView);
                if (binding == null) return;
                binding.setVariable(BR.viewModel, entity);
                binding.executePendingBindings();
            }

        }
    }
}
